/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.DELETE;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.WebApplicationException;
import javax.persistence.NoResultException;
import javax.persistence.EntityManager;
import jpa.entities.Usuario;
import converter.UsuarioAdmConverter;
import javax.ejb.Stateless;
import jpa.entities.UsuarioAdm;

/**
 *
 * @author carlos
 */

@Stateless
public class UsuarioAdmResource {
    @javax.ejb.EJB
    private UsuarioResourceSub usuarioResourceSub;
    @Context
    protected UriInfo uriInfo;
    protected EntityManager em;
    protected Integer id;
  
    /** Creates a new instance of UsuarioAdmResource */
    public UsuarioAdmResource() {
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * Get method for retrieving an instance of UsuarioAdm identified by id in XML format.
     *
     * @param id identifier for the entity
     * @return an instance of UsuarioAdmConverter
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public UsuarioAdmConverter get(@QueryParam("expandLevel")
                                   @DefaultValue("1")
    int expandLevel) {
        return new UsuarioAdmConverter(getEntity(), uriInfo.getAbsolutePath(), expandLevel);
    }

    /**
     * Put method for updating an instance of UsuarioAdm identified by id using XML as the input format.
     *
     * @param id identifier for the entity
     * @param data an UsuarioAdmConverter entity that is deserialized from a XML stream
     */
    @PUT
    @Consumes({"application/xml", "application/json"})
    public void put(UsuarioAdmConverter data) {
        updateEntity(getEntity(), data.resolveEntity(em));
    }

    /**
     * Delete method for deleting an instance of UsuarioAdm identified by id.
     *
     * @param id identifier for the entity
     */
    @DELETE
    public void delete() {
        deleteEntity(getEntity());
    }

    /**
     * Returns an instance of UsuarioAdm identified by id.
     *
     * @param id identifier for the entity
     * @return an instance of UsuarioAdm
     */
    protected UsuarioAdm getEntity() {
        try {
            return (UsuarioAdm) em.createQuery("SELECT e FROM UsuarioAdm e where e.idAdm = :idAdm").setParameter("idAdm", id).getSingleResult();
        } catch (NoResultException ex) {
            throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
        }
    }

    /**
     * Updates entity using data from newEntity.
     *
     * @param entity the entity to update
     * @param newEntity the entity containing the new data
     * @return the updated entity
     */
    private UsuarioAdm updateEntity(UsuarioAdm entity, UsuarioAdm newEntity) {
        Usuario usuario = entity.getUsuario();
        Usuario usuarioNew = newEntity.getUsuario();
        entity = em.merge(newEntity);
        if (usuario != null && !usuario.equals(usuarioNew)) {
            usuario.getUsuarioAdmCollection().remove(entity);
        }
        if (usuarioNew != null && !usuarioNew.equals(usuario)) {
            usuarioNew.getUsuarioAdmCollection().add(entity);
        }
        return entity;
    }

    /**
     * Deletes the entity.
     *
     * @param entity the entity to deletle
     */
    private void deleteEntity(UsuarioAdm entity) {
        Usuario usuario = entity.getUsuario();
        if (usuario != null) {
            usuario.getUsuarioAdmCollection().remove(entity);
        }
        em.remove(entity);
    }

    /**
     * Returns a dynamic instance of UsuarioResource used for entity navigation.
     *
     * @param id identifier for the parent entity
     * @return an instance of UsuarioResource
     */
    @Path("usuario/")
    public UsuarioResource getUsuarioResource() {
        usuarioResourceSub.setParent(getEntity());
        return usuarioResourceSub;
    }

    @Stateless(name = "UsuarioAdmResource.UsuarioResourceSub")
    public static class UsuarioResourceSub extends UsuarioResource {

        private UsuarioAdm parent;

        public void setParent(UsuarioAdm parent) {
            this.parent = parent;
        }

        @Override
        protected Usuario getEntity() {
            Usuario entity = parent.getUsuario();
            if (entity == null) {
                throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
            }
            return entity;
        }
    }
}
